import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import edu.cornell.cs4740.sentencegenerator.BigramTable;
import edu.cornell.cs4740.sentencegenerator.Perplexity;
import edu.cornell.cs4740.sentencegenerator.Utilities;

public class Main {
  /**
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    Utilities.Smoothing smoothing = Utilities.Smoothing.NONE;

    // get smoothing preferences: Laplacian, Good-Turing, or none
    System.out.println("Use smoothing? y or n");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String answer = br.readLine();
    if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")) {
      boolean needAnswer = true;
      while (needAnswer) {
        System.out
            .println("Use Laplacian smoothing or Good-Turing discounting? l or g");
        answer = br.readLine();
        if (answer.equalsIgnoreCase("l") || answer.equalsIgnoreCase("lap")
            || answer.equalsIgnoreCase("laplacian")) {
          smoothing = Utilities.Smoothing.LAPLACIAN;
          needAnswer = false;
        } else if (answer.equalsIgnoreCase("g")
            || answer.equalsIgnoreCase("gt")
            || answer.equalsIgnoreCase("good turing")
            || answer.equalsIgnoreCase("good-turing")) {
          smoothing = Utilities.Smoothing.GOODTURING;
          needAnswer = false;
        }
      }
    }

    // get datasets to use
    System.out.println("What dataset should be used first? 1, 2, 3, or 4");
    answer = br.readLine();
    String dataNumber1 = answer;
//    String datapath1 = "data/Dataset" + answer + "/train.txt";
    String datapath1 = "data/sample1/train.txt";/**REMOVE*/
    // List<String> dataset1 = Utilities.tokenizeFile(datapath1, true);
    System.out.println("What dataset should be used second? 1, 2, 3, or 4");
    answer = br.readLine();
    String dataNumber2 = answer;
//    String datapath2 = "data/Dataset" + answer + "/train.txt";
    String datapath2 = "data/sample3/train.txt";/**REMOVE*/
    // List<String> dataset2 = Utilities.tokenizeFile(datapath2, true);

    for (int i = 1; i < 3; i++) {
      System.out.println("Building table...");
      // make bigram and unigram table using smoothing preferences
      String path = (i == 1) ? datapath1 : datapath2;
      List<String> dataset = Utilities.tokenizeFile(path, true);
      BigramTable table = null;
      switch (smoothing) {
      case NONE:
        table = new BigramTable(dataset, false);// no unknown word handling
        break;
      case LAPLACIAN:
        table = new BigramTable(dataset, true); // unknown word handling
        // table.laplacian();
        break;
      case GOODTURING:
        table = new BigramTable(dataset, true); // unknown word handling
        // table.goodturing();
        break;
      }

      // generate random sentences
      System.out.println("Generate random sentences? y or n");
      answer = br.readLine();
      if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")) {
        // generate random sentences
      }

      // compute perplexity
      System.out.println("Compute perplexity? y or n");
      answer = br.readLine();
      if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")) {
//        String testPath1 = "data/Dataset" + dataNumber1 + "/test.txt";
//        String testPath2 = "data/Dataset" + dataNumber2 + "/test.txt";
        String testPath1 = "data/sample1/test.txt";/**REMOVE*/
        String testPath2 = "data/sample3/test.txt";/**REMOVE*/
        System.out.println("Model: " + path);
        for (int j = 1; j < 3; j++) {
          String testpath = (j == 1) ? testPath1 : testPath2;
          List<String> testSet = Utilities.tokenizeFile(testpath, true);
          Perplexity p = new Perplexity(table);
          double perplexity = p.getPerplexity(testSet, smoothing);
          System.out.println("   Perplexity = " + perplexity + " for " + testpath);
        }
      }
    }

  }

}
